<template>
  <view class="cart-container" v-if="cart.length !== 0">
    <my-address></my-address>
    <view class="cart-title">
      <!-- 左侧的图标 -->
      <uni-icons type="shop" size="18"></uni-icons>
      <!-- 描述文本 -->
      <text class="cart-title-text">购物车</text>
    </view>
    <uni-swipe-action>
      <block v-for="(goods, i) in cart" :key="i">
        <uni-swipe-action-item :right-options="options" @click="swipeActionClickHandler(goods)">
          <view class="goods-item">
            <!-- 商品左侧图片区域 -->
            <view class="goods-item-left">
              <radio color="#C00000" :checked="goods.goods_state" @click="radioChangeHandler(goods)"></radio>
              <image :src="goods.goods_small_logo || defaultPic" class="goods-pic"></image>
            </view>
            <!-- 商品右侧信息区域 -->
            <view class="goods-item-right">
              <!-- 商品标题 -->
              <view class="goods-name">{{goods.goods_name}}</view>
              <!-- 管道运算符，前面的结果是后面 的参数 -->
              <view class="goods-info-box">
                <!-- 商品价格 -->
                <view class="goods-price">￥{{goods.goods_price | tofixed}}</view>
                <!-- 商品数量 -->
                <view class="help" @click="GetIdHandler(goods)">
                  <uni-number-box :min="1" :value="goods.goods_count" @change="numChangeHandler"></uni-number-box>
                </view>
              </view>
            </view>
          </view>
        </uni-swipe-action-item>
      </block>
    </uni-swipe-action>
    <!-- 结算区域 -->
    <my-settle></my-settle>
  </view>
  <!-- 空白购物车区域 -->
  <view class="empty-cart" v-else>
    <image src="/static/cart_empty@2x.png" class="empty-img"></image>
    <text class="tip-text">空空如也~</text>
  </view>
</template>

<script>
  import {
    mapState,
    mapMutations,
    mapGetters
  } from 'vuex'
  import badgeMix from '@/mixins/tabbar-badge.js'
  export default {
    mixins: [badgeMix],
    data() {
      return {
        temp_goods_id: -1,
        temp_goods_count: 0,
        options: [{
          text: "删除",
          style: {
            backgroundColor: "#c00000"
          }
        }]
      }
    },
    filters: {
      tofixed(num) {
        return Number(num).toFixed(2)
      }
    },
    onLoad() {
      // console.log("address",this.address)
    },
    computed: {
      ...mapState('m_cart', ['cart']),
      ...mapGetters('m_cart', ['total']),
      // ...mapState('m_user', ['address']),
    },
    methods: {
      ...mapMutations('m_cart', ['updateGoodsState', 'updateGoodsCount', 'removeGoodsById']),
      radioChangeHandler(goods) {
        // console.log('goods1',goods)
        // 在这里直接改仓库中的数据是无效的
        // goods.goods_state=!goods.goods_state
        const temp = {
          goods_id: goods.goods_id,
          goods_state: !goods.goods_state
        }
        this.updateGoodsState(temp)
      },
      numChangeHandler(num) {
        // console.log("num",num)
        // console.log("this",this)
        // console.log("goods",goods)
        // const temp={
        //   goods_count:this.goods
        // }
        this.temp_goods_count = num
        if ((this.temp_goods_id !== -1) && (this.temp_goods_count !== 0)) {
          const temp = {
            goods_id: this.temp_goods_id,
            goods_count: this.temp_goods_count
          }
          this.updateGoodsCount(temp)
          this.temp_goods_id = -1
          this.temp_goods_count = 0
        }
      },
      // 这里用事件的冒泡来获取获取商品id
      GetIdHandler(goods) {
        // this.temp_goods_id=goods.goods_id
        // console.log(goods)
        this.temp_goods_id = goods.goods_id
        if ((this.temp_goods_id !== -1) && (this.temp_goods_count !== 0)) {
          const temp = {
            goods_id: this.temp_goods_id,
            goods_count: this.temp_goods_count
          }
          this.updateGoodsCount(temp)
          this.temp_goods_id = -1
          this.temp_goods_count = 0
        }
      },
      swipeActionClickHandler(goods) {
        this.removeGoodsById(goods.goods_id)
        // console.log(goods)
      }
    },
    // watch:{
    //   total:{
    //     handler(){
    //       // 这里可以直接调用混入里面的函数
    //       this.setBadge()
    //     }
    //   }
    // }
  }
</script>

<style>
  .cart-container {
    padding-bottom: 50px;
  }

  .cart-title {
    height: 40px;
    display: flex;
    align-items: center;
    font-size: 14px;
    padding-left: 5px;
    border-bottom: 1px solid #efefef;
  }

  .cart-title-text {
    margin-left: 10px;
  }

  .goods-item {
    width: 750rpx;
    box-sizing: border-box;
    display: flex;
    padding: 10px 5px;
    border-bottom: 1px solid #f0f0f0;
  }

  .goods-item-left {
    margin-right: 5px;
    display: flex;
    flex: 1;
    justify-content: space-between;
    align-items: center;
  }

  .goods-pic {
    width: 100px;
    height: 100px;
    display: block;
  }

  .goods-info-box {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .goods-item-right {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
  }

  .goods-name {
    font-size: 13px;
  }

  .goods-price {
    font-size: 16px;
    color: #c00000;
  }

  /* .help{
    position: absolute;
    width: 100%;
    height: 5%;
    background-color: #C00000; 
    z-index: 1000000;
  } */
  .empty-cart {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-top: 150px;
  }

  .empty-img {
    width: 90px;
    height: 90px;
  }

  .tip-text {
    font-size: 12px;
    color: gray;
    margin-top: 15px;
  }
</style>
